package com.taxi.remote;

import com.taxi.constant.AmapConfigConstants;
import com.taxi.response.ServiceResponse;
import com.taxi.result.Result;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @Author: aniu
 * @Date: 2023/9/30
 * @ClassName: ServiceClient
 * @Description: Write class functionality and purpose here.
 * @Version: 1.0
 */
@Service
public class ServiceClient {
    @Value("${amap.key}")
    private String amapKey;

    @Autowired
    private RestTemplate restTemplate;

    public Result add(String name){
        //拼装请求的url
        StringBuilder url = new StringBuilder();
        url.append(AmapConfigConstants.SERVICE_ADD_URL);
        url.append("?");
        url.append("key="+amapKey);
        url.append("&");
        url.append("name="+name);

        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(url.toString(),null, String.class);
        /**
         * {
         *     "errcode": 10000,
         *     "errmsg": "OK",
         *     "data": {
         *         "name": "无忧出行",
         *         "sid": 1007629
         *     }
         * }
         */
        String body = stringResponseEntity.getBody();
        JSONObject result = JSONObject.fromObject(body);
        JSONObject data = result.getJSONObject("data");
        Integer sid = data.getInt("sid");
        ServiceResponse serviceResponse = new ServiceResponse();
        serviceResponse.setSid(sid+"");

        return Result.ok(serviceResponse);
    }
}
